package io.esirong.network.exception;

import android.content.Context;
import android.net.ParseException;
import android.util.Log;

import androidx.annotation.StringRes;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;

import javax.net.ssl.SSLPeerUnverifiedException;

import io.esirong.network.R;

/**
 *
 */
public class ExceptionHandler {

    public final String SUCCESS = "00000";

    /**
     * 处理HTTP CODE
     *
     * @param context
     * @param httpCode
     */
    public static void showError(Context context, int httpCode) {
        if (httpCode == HttpURLConnection.HTTP_OK) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_ACCEPTED) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else if (httpCode == HttpURLConnection.HTTP_BAD_REQUEST) {

        } else {
        }
    }

    /**
     * 处理响应
     *
     * @param context
     * @param RespCode
     */
    public static void showError(Context context, String RespCode) {

    }

    /**
     * 处理异常
     *
     * @param context
     * @param e
     * @return
     */
    public static Throwable handleError(Context context, java.lang.Throwable e) {
        Log.e("ExceptionHandler", e.getMessage());
        String detail = "";
        if (e.getCause() != null) {
            detail = e.getCause().getMessage();
        }
        Log.e("ExceptionHandler", detail);

        Throwable ex;
        if (e instanceof SocketTimeoutException) {
            ex = new Throwable(e, ERROR.TIMEOUT_ERROR);
            ex.setMessage(context.getString(R.string.ex_msg_time_out));
            return ex;
        } else if (e instanceof java.net.ConnectException) {
            ex = new Throwable(e, ERROR.NET_ERROR);
            ex.setMessage("无法连接到服务器");
            return ex;
        } else if (e instanceof java.net.UnknownHostException) {
            ex = new Throwable(e, HttpURLConnection.HTTP_NOT_FOUND);
            ex.setMessage("未知服务器");
            return ex;
        } else if (e instanceof java.net.MalformedURLException) {
            ex = new Throwable(e, ERROR.HTTP_ERROR);
            ex.setMessage("无效的链接格式");
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new Throwable(e, ERROR.PARSE_ERROR);
            ex.setMessage("解析错误");
            return ex;

        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new Throwable(e, ERROR.SSL_ERROR);
            ex.setMessage("证书验证失败");
            return ex;
        } else if (e instanceof java.security.cert.CertPathValidatorException) {
            ex = new Throwable(e, ERROR.SSL_NOT_FOUND);
            ex.setMessage("证书路径没找到");

            return ex;
        } else if (e instanceof SSLPeerUnverifiedException) {
            ex = new Throwable(e, ERROR.SSL_NOT_FOUND);
            ex.setMessage("无有效的SSL证书");
            return ex;

        }

        ex = new Throwable(e, ERROR.UNKNOWN);
        ex.setMessage(e.getMessage());
        return ex;
    }


    public static Throwable handleException(java.lang.Throwable e) {
        Throwable ex;
        ex = new Throwable(e, ERROR.UNKNOWN);
        return ex;
    }

    /**
     * 约定异常
     */
    public interface ERROR {
        /**
         * 未知错误
         */
        int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        int PARSE_ERROR = 1001;
        /**
         * 网络错误
         */
        int NET_ERROR = 1002;
        /**
         * 协议出错
         */
        int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        int SSL_ERROR = 1005;

        /**
         * 连接超时
         */
        int TIMEOUT_ERROR = 1006;

        /**
         * 证书未找到
         */
        int SSL_NOT_FOUND = 1007;

        /**
         * 出现空值
         */
        int NULL = -100;

        /**
         * 格式错误
         */
        int FORMAT_ERROR = 1008;
    }

}
